package
{

    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.events.*;
    import flash.utils.*;

    import events.SoundEvent;    
    import assets.Assets;
    import flash.media.Sound;
	
    import logic.map.MapBuilder;
    import logic.characters.*;
    import logic.obstacles.*;
    import levels.*;
    

    [SWF(width = '640', height = '480', backgroundColor = '#ffffff', frameRate = '60')]
    public class Main extends Sprite
    {
        private const WIDTH:uint = 640;
        private const HEIGHT:uint = 480;
        
        private var background:Sprite;
        private var paralax:Bitmap;
        private var soundManager:SoundManager;
        
        private var mainCharacter:Player;
        private var image:Bitmap = new Bitmap(MapBuilder.buildMapBitmap(1)); 
        private var mcd:Sprite = new Sprite();
        private var keyDict:Dictionary = new Dictionary();
        
        private var level:LevelModel;
        private var currentLevel = 0;
        private var levels:Array = [new Level1(), new Level2(), new Level3()];
        
        var enemies:Sprite;
        
        public function Main()
        {
            enemies = new Sprite;
            
            soundManager = new SoundManager();
	        initSounds();
		
		    this.addEventListener(SoundEvent.PLAY_SOUND, playSound);
		    this.addEventListener(SoundEvent.STOP_SOUND, stopSound);		
            
            recreateLevel();
        }
        
        private function recreateLevel():void
        {

             removeEventListener(Event.ENTER_FRAME, onEnterFrame);
             while (this.numChildren > 0) {
                this.removeChildAt(0);
             }
             
             level = levels[currentLevel];
             paralax = BackgroundCreator.buildBackground(level.levelWidth,level.levelHeight);
             this.addChild(paralax);
             
             
             mainCharacter = new Player();
             mainCharacter.addEventListener("killed", onKill);
             mainCharacter.x = 170; //level.playerX;
             mainCharacter.y = 200; //level.playerY;
             mainCharacter.levelBottom = level.levelHeight;
             addChild(mainCharacter);
             
             var obj:Array =  level.buildBackground();
             background = obj[0];
             
             enemies = level.buildEnemies();
             
             addChild(enemies);

            //Comenteaza aici
             addChild(obj[0]);
             addChild(obj[1]);
             
             
             addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        private function onKill(event:Event = null)
        {
            recreateLevel();
        }
        
        private function onEnterFrame(event:Event = null) 
        {            
            level.update();
            mainCharacter.update(background, enemies)
            trace(mainCharacter.x+" "+mainCharacter.y);
            
            this.x = -mainCharacter.x + 320;
            this.y = -mainCharacter.y + 240;
   
            paralax.x = this.x*-0.8;
            paralax.y = this.y*-0.8 - (level.levelHeight-500);
        }
        
        private function initSounds():void
		{
			var sounds:Dictionary = new Dictionary()			
			sounds["jump"] =  new Assets.Jump() as Sound;
			sounds["electric"] =  new Assets.Electric() as Sound;
			sounds["die"] =  new Assets.Die() as Sound;
			
			var volumes:Dictionary = new Dictionary()
		    volumes["jump"] = 1;
		    volumes["electric"] = .1;
		    volumes["die"] = .5;
					
			soundManager.loadSounds(sounds, volumes);
			soundManager.oneAtATime = ["jump"];
		}	
		
		private function playSound(e:SoundEvent):void
		{	
			soundManager.playSound(e.dataObj.sound, -1, e.dataObj.offset, e.dataObj.repeat);
		}	
		
		private function stopSound(e:SoundEvent):void
		{	
			soundManager.stopSound(e.dataObj.sound);
		}		
    }
}
